#---------------------------------------------------------------------
# Makefile for Greenplum Database
#
#---------------------------------------------------------------------

# keep the default target above any targets from included makefiles
all : devel

#---------------------------------------------------------------------
# Declare the targets
#---------------------------------------------------------------------

# Targets for building GPDB with various options
# (development builds)
.PHONY : devel devel_faultinj 

# (release builds)
.PHONY : dist dist_prof 

# Targets which refresh files via rsync before building
.PHONY : dbg opt both

# Targets recognized by the PostgreSQL makefile (GNUmakefile)
.PHONY : all install installdirs uninstall distprep
.PHONY : clean distclean maintainer-clean
.PHONY : check installcheck installcheck-parallel

# Targets for running regression tests
.PHONY : installcheck-good installcheck-olaptest installcheck-bugbuster

# The pvk target builds a subset of the GPDB package that includes just the
# tools used to verify the configuration of a set of GPDB servers.
.PHONY : pvk

# Targets for running autoconf and autoheader
.PHONY : autoconf

# Internal functions which are invoked by other rules within this makefile
.PHONY : groupSession mkgpfdist mkgpperfmon copydocs mgmtcopy copylibs copyoptimizerlibs
.PHONY : greenplum_path RECONFIG HOMEDEP GPROOTDEP GPROOTDEP GPROOTFAIL
.PHONY : gccVersionCheck clients loaders connectivity gppkg

#---------------------------------------------------------------------
# Make 3rd-party libraries and tools
#---------------------------------------------------------------------

# tell included makefiles where to find build-top-level resources
BLD_TOP:=$(shell sh -c pwd)
include Makefile.global
include Makefile.thirdparty
include releng/releng.mk

ifneq "$(BLD_BITS)" ""
export NO_M64=1
endif

#$(warning )
#$(warning PATH=$(PATH))
#$(warning LD_LIBRARY_PATH=$(LD_LIBRARY_PATH))
#$(warning )
#$(warning BLD_ARCH=$(BLD_ARCH))
#$(warning BLD_BITS=$(BLD_BITS))
#$(warning BLD_CFLAGS=$(BLD_CFLAGS))
#$(warning )

# help gpcheckdb, our only C++ binary, find the right copy of libstdc++
sol10_sparc_32_CXX_LIB_DIR=/usr/sfw/lib
BLD_CXX_LIB_DIR=$($(BLD_ARCH)_CXX_LIB_DIR)

# help Windows builds find the cross-compiler and a pre-built curl
ifeq "$(findstring win,$(BLD_ARCH))" "win"
tmpPATH:=/usr/local/mingw32/bin:/usr/local/mingw32/mingw32/bin:/opt/curl-7.16.1-crosscompile/bin:$(PATH)
export PATH:=$(tmpPATH)
export WINDOWS=true
export ANT_OPTS="-DminGW=true"
export SCRIPT_EXT=bat
export EXE_EXT=.exe
export PY_EXT=.py
endif

#---------------------------------------------------------------------
# Directory paths and file names
#---------------------------------------------------------------------

GPPGDIR=..
GPMGMT=../gpMgmt
GPPERFMON=gpperfmon
PLATFORM=platform
BUILDDIR=$(GPPGDIR)

RELEASE=devel
GPDIR=greenplum-db-$(RELEASE)
PLRDIR=greenplum-plr-$(RELEASE)
CLIENTDIR=greenplum-clients-$(RELEASE)
CONNECTIVITYDIR=greenplum-connectivity-$(RELEASE)
LOADERSDIR=greenplum-loaders-$(RELEASE)
GPDOCDIR=$(CURDIR)/docs/release

DISTPATH=$(GPROOT)/$(GPDIR)
CLIENTDISTPATH=$(GPROOT)/$(CLIENTDIR)
CONNECTIVITYDISTPATH=$(GPROOT)/$(CONNECTIVITYDIR)
LOADERSDISTPATH=$(GPROOT)/$(LOADERSDIR)
GPPERFMONDISTPATH=$(GPROOT)
GPPKGDISTPATH=$(GPROOT)

ifeq "$(DEVPATH)" ""
BLD_HOME=$(HOME)
DEVPATH=$(BLD_HOME)/$(GPDIR)
GPPERFMONDEVPATH=$(BLD_HOME)
GPPKGDEVPATH=$(BLD_HOME)
CLIENTDEVPATH=$(BLD_HOME)/$(CLIENTDIR)
CONNECTIVITYDEVPATH=$(BLD_HOME)/$(CONNECTIVITYDIR)
LOADERSDEVPATH=$(BLD_HOME)/$(LOADERSDIR)
else
# DEVPATH has been passed in as by the build scripts
GPROOT_DEV=$(dir $(DEVPATH))
GPPERFMONDEVPATH=$(GPROOT_DEV)
GPPKGDEVPATH=$(GPROOT_DEV)
CLIENTDEVPATH=$(GPROOT_DEV)$(CLIENTDIR)
CONNECTIVITYDEVPATH=$(GPROOT_DEV)$(CONNECTIVITYDIR)
LOADERSDEVPATH=$(GPROOT_DEV)$(LOADERSDIR)
endif

# files generated by 'configure'
ISCONFIG=../GNUmakefile


##
## On these platforms, we do the full build including the server. On other
## platforms, we do a client-only build.
##
SERVER_PLATFORMS=rhel5_x86_32 rhel5_x86_64 rhel6_x86_64 suse10_x86_64 suse11_x86_64 linux_x86_64 osx106_x86

#---------------------------------------------------------------------
# GPPKG
#---------------------------------------------------------------------

##
## Support gppkg platforms
##

GPPKG_PLATFORMS=rhel5_x86_64 rhel6_x86_64 suse10_x86_64 suse11_x86_64

#---------------------------------------------------------------------
# Compiler options
#---------------------------------------------------------------------

INLINE_FLAGS=-finline-limit=1800

OPTFLAGS="$(strip -O3 -funroll-loops -fargument-noalias-global -fno-omit-frame-pointer $(COPTX) -g $(INLINE_FLAGS) $(TRACE_GCC_FLAG))"
PROFFLAGS="$(strip -O3 -funroll-loops -fargument-noalias-global -fno-omit-frame-pointer $(COPTX) -g $(INLINE_FLAGS) $(TRACE_GCC_FLAG))"

ifeq (on, ${GPDBGINLINE})
CFLAGS_INLINE=$(INLINE_FLAGS)
else
CFLAGS_INLINE=-fno-inline
endif

ifeq (on, ${GPDBGOPT})
CFLAGS_OPT=-O1 -fno-omit-frame-pointer
else
CFLAGS_OPT=-O0
endif

ifeq (on, ${GPDBGCOV})
DEBUG_CFLAGS_OPT=
else
DEBUG_CFLAGS_OPT=-g3
endif

DEBUGFLAGS="$(strip $(CFLAGS_INLINE) $(CFLAGS_OPT) $(DEBUG_CFLAGS_OPT) $(TRACE_GCC_FLAG))"

#---------------------------------------------------------------------
# configure
#---------------------------------------------------------------------

DEFPORT=5432

ORCA_CONFIG=--enable-orca
win32_CONFIGFLAGS=--with-gssapi --without-libcurl
rhel5_x86_32_CONFIGFLAGS=--host=i686-pc-linux-gnu --enable-snmp --enable-ddboost --with-gssapi --enable-netbackup ${ORCA_CONFIG} --with-libxml
rhel5_x86_64_CONFIGFLAGS=--enable-snmp --enable-connectemc --enable-ddboost --with-gssapi --enable-netbackup ${ORCA_CONFIG} --with-libxml
rhel6_x86_64_CONFIGFLAGS=--enable-snmp --enable-connectemc --enable-ddboost --with-gssapi --enable-netbackup ${ORCA_CONFIG} --with-libxml
sol10_x86_64_CONFIGFLAGS=--enable-snmp ${ORCA_CONFIG} --with-libxml
sol_x86_64_CONFIGFLAGS=--without-libcurl
suse10_x86_64_CONFIGFLAGS=--enable-snmp --enable-ddboost ${ORCA_CONFIG} --with-libxml
suse11_x86_64_CONFIGFLAGS=--enable-snmp --enable-ddboost ${ORCA_CONFIG} --with-libxml
linux_x86_64_CONFIGFLAGS=--enable-snmp ${ORCA_CONFIG} --with-libxml
osx106_x86_CONFIGFLAGS=--enable-snmp ${ORCA_CONFIG} --with-libxml

BLD_CONFIGFLAGS=$($(BLD_ARCH)_CONFIGFLAGS)

CONFIGFLAGS=$(strip $(BLD_CONFIGFLAGS) --with-pgport=$(DEFPORT) --with-libedit-preferred $(TRACE_CFG_FLAG) $(BLD_DEPLOYMENT_SETTING)) 

# PL/Python does not compile on Tiger, skipping it on AIX
ifneq "$(findstring $(BLD_ARCH),aix5_ppc_64 aix5_ppc_32)" ""
PG_LANG=false
endif

ifneq (false, ${PG_LANG})
ifeq "$(findstring win,$(BLD_ARCH))" "win"
CONFIGFLAGS+= --with-perl --with-python 
else
CONFIGFLAGS+= --with-perl --with-python --with-java
endif 
ifdef TCL_CFG
CONFIGFLAGS+= --with-tcl-config=${TCL_CFG}
endif
ifneq "$(wildcard $(R_HOME))" ""
CONFIGFLAGS+= --with-r
endif
endif

# Configure in the extra path AIX requires for pthread support
aix5_ppc_64_PTHREADS_LIB_DIR=/usr/lib/threads
aix5_ppc_32_PTHREADS_LIB_DIR=/usr/lib/threads
BLD_PTHREADS_LIB_DIR=$($(BLD_ARCH)_PTHREADS_LIB_DIR)

ADDON_DIR=addon

ifdef ADDON_DIR
ADDON_CONFIG_INCLUDES=$(BLD_TOP)/$(ADDON_DIR)/src/include
endif


# Configure in thirdparty libraries from the ext/ directory
CONFIG_INCLUDES=$(BLD_THIRDPARTY_INCLUDE_DIR) $(BLD_THIRDPARTY_INCLUDE_DIR)/libxml2 $(ADDON_CONFIG_INCLUDES)
CONFIG_LIBRARIES=$(strip $(BLD_THIRDPARTY_LIB_DIR) $(BLD_CXX_LIB_DIR) $(BLD_PTHREADS_LIB_DIR))
CONFIGFLAGS+= --with-includes="$(CONFIG_INCLUDES)" --with-libraries="$(CONFIG_LIBRARIES)"

# Configure in "authlibs"...
# ...without an ext/ dir copy of curl-config on Tiger or AIX
ifeq "$(findstring $(BLD_ARCH),aix5_ppc_64 aix5_ppc_32)" ""
BLD_CURL_CONFIG=CURL_CONFIG=$(BLD_THIRDPARTY_BIN_DIR)/curl-config
endif
# ...and do not include the authlibs on Windows or AIX
ifeq "$(findstring $(BLD_ARCH),aix5_ppc_32 aix5_ppc_64 win32 win64)" ""
CONFIGFLAGS+= --with-openssl --with-pam --with-krb5 --with-ldap $(BLD_CURL_CONFIG)
endif
# ...but do include some of the authlibs on AIX
ifneq "$(findstring $(BLD_ARCH),aix5_ppc_32 aix5_ppc_64)" ""
CONFIGFLAGS+= --with-openssl --with-pam --without-krb5 --with-ldap $(BLD_CURL_CONFIG)
endif

# Platform-specific config flags
aix5_ppc_64_CONFIG_ADDITIONS=LDFLAGS="-Wl,-bbigtoc -L/usr/lib/threads"
aix5_ppc_32_CONFIG_ADDITIONS=LDFLAGS="-L/usr/lib/threads"
hpux_ia64_CONFIG_ADDITIONS=--without-readline --without-ldap
win32_CONFIG_ADDITIONS=--build=x86_x64-unknown-linux-gnu --host=i686-pc-mingw32 --without-zlib --enable-debug CC=/usr/bin/i686-pc-mingw32-gcc LDFLAGS="-L/usr/i686-pc-mingw32/sys-root/mingw/lib -L$(BLD_TOP)/ext/win32/kfw-3-2-2/lib" CPP= CPPFLAGS="-I/usr/i686-pc-mingw32/sys-root/mingw/include -I$(BLD_TOP)/ext/win32/kfw-3-2-2/inc/krb5"
win64_CONFIG_ADDITIONS=--build=x86_x64-unknown-linux-gnu --host=x86_64-pc-mingw64 --without-zlib --enable-debug CC=/usr/local/mingw64/bin/x86_64-w64-mingw32-gcc LDFLAGS=-L/usr/local/mingw64/x86_64-w64-mingw32/lib CPP= CPPFLAGS=-I/usr/local/mingw64/x86_64-w64-mingw32/include
CONFIG_ADDITIONS=$($(BLD_ARCH)_CONFIG_ADDITIONS)

CONFIGFLAGS+= $(CONFIG_ADDITIONS)

ifdef ADDON_DIR
CONFIGFLAGS+=ADDON_DIR=$(ADDON_DIR)
endif


RECONFIG :
	rm -f Debug/GNUmakefile
	rm -f Release/GNUmakefile
	rm -f $(GPPGDIR)/GNUmakefile

# avoid AIX's restricted shell
aix5_ppc_64_CONFIG_SHELL=/usr/bin/bash
aix5_ppc_32_CONFIG_SHELL=/usr/bin/bash
BLD_CONFIG_SHELL=$($(BLD_ARCH)_CONFIG_SHELL)

$(GPPGDIR)/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p $(GPPGDIR) 
	cd $(GPPGDIR) && CC="$(strip $(BLD_CC) $(BLD_CFLAGS))" CFLAGS=$(INSTCFLAGS) $(BLD_CONFIG_SHELL) ./configure $(CONFIGFLAGS) --prefix=$(INSTLOC) --with-docdir=$(INSTLOC)/doc --mandir=$(INSTLOC)/man

Debug/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p Debug 
	cd Debug && CC="$(strip $(BLD_CC) $(BLD_CFLAGS))" CFLAGS=$(INSTCFLAGS) $(BLD_CONFIG_SHELL) ./configure $(CONFIGFLAGS) --prefix=$(INSTLOC) --with-docdir=$(INSTLOC)/doc --mandir=$(INSTLOC)/man

Release/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p Release 
	cd Release && CC="$(strip $(BLD_CC) $(BLD_CFLAGS))" CFLAGS=$(INSTCFLAGS) $(BLD_CONFIG_SHELL) ./configure $(CONFIGFLAGS) --prefix=$(INSTLOC) --with-docdir=$(INSTLOC)/doc --mandir=$(INSTLOC)/man

## ======================================================================
## Generate json catalog file
##
## Requirement:
##   This tidycat.pl utility has a perl JSON package dependency.  You'll
##   need to ensure you have this setup in your enviroment to execute
##   this process.
##
## Here are the basic steps:
##
##   1) A "configure" must have been run.  This is required so
##      GP_MAJORVERSION can be retrieved from the
##      src/include/pg_config.h and used to create the
##      json file name (eg. 4.2.json).
##   2) (manual) p4 edit gpMgmt/bin/gppylib/data/<version>.json
##   3) make gen_json_cat
##   4) (manual file generation review) p4 diff gpMgmt/bin/gppylib/data/<version>.json
##   5) (manual) p4 submit "Regenerate Json catalog file." gpMgmt/bin/gppylib/data/<version>.json
## ======================================================================

gen_json_cat:
	@if [ ! -f $(ISCONFIG) ]; then echo ""; echo "FATAL: The cdb-pg environment has not been configured.  Thus we cannot attempt to generate json catalog file."; echo ""; exit 1; fi
	@JASON_CAT=gpMgmt/bin/gppylib/data/`grep GP_MAJORVERSION ../src/include/pg_config.h | sed 's/.*"\(.*\)"/\1/g' `.json; \
	if [ ! -w $$JASON_CAT ]; then echo ""; echo "FATAL: The json cat file is not writable ($$JASON_CAT).  Have you performed a p4 edit $$JASON_CAT operation?"; echo ""; exit 1; fi; \
	cd $(BLD_TOP)/../src/include/catalog; \
	perl tidycat.pl -dd $(BLD_TOP)/$$JASON_CAT -df json *.h; \
	if [ $$? != 0 ]; then echo ""; echo "FATAL: json cat file generation failed."; echo ""; exit 1; fi; \
	echo "To manually review and submit the updated json cat file, run:"; \
	echo "  p4 diff $$JASON_CAT"; \
	echo "  p4 submit -d \"Regenerate Json catalog file. [JIRA: MPP-XXXX]\" $$JASON_CAT"; \

#---------------------------------------------------------------------
# autoconf
#---------------------------------------------------------------------
# Whenever there is a change to the configure.in file, autoconf and
# autoheader should be run to regenerate the files configure and
# src/include/pg_config.h.in.  Note that autoconf and autoheader
# don't update the timestamps of their output files unless there is an
# actual change in the contents; thus 'make' rules don't work well for
# rebuilding those files automatically.  Developers should rebuild them
# manually when needed, which can be done by invoking 'make autoconf'.

autoconf : RECONFIG
	cd $(GPPGDIR) && autoconf && autoheader

#---------------------------------------------------------------------
# standardized build rules
#---------------------------------------------------------------------

rhel5_x86_32_LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):/usr/lib
BLD_LD_LIBRARY_PATH:=$($(BLD_ARCH)_LD_LIBRARY_PATH):$($(BLD_WHERE_THE_LIBRARY_THINGS_ARE))
export $(BLD_WHERE_THE_LIBRARY_THINGS_ARE)=$(BLD_LD_LIBRARY_PATH)

# how much of GPDB to build by platform (default is "full")
hpux_ia64_GPDB_BUILDSET=partial
rhel5_x86_32_GPDB_BUILDSET=partial
aix5_ppc_64_GPDB_BUILDSET=partial
aix5_ppc_32_GPDB_BUILDSET=partial
sol10_x86_64_GPDB_BUILDSET=partial
sol10_x86_32_GPDB_BUILDSET=partial
sol10_sparc_32_GPDB_BUILDSET=partial
win64_GPDB_BUILDSET=partial
win32_GPDB_BUILDSET=partial
BLD_GPDB_BUILDSET=$($(BLD_ARCH)_GPDB_BUILDSET)

# set default build steps
define BUILD_STEPS
	@rm -rf $(INSTLOC)
	cd $(BUILDDIR) && $(MAKE) $(parallelexec_opts) install
	#@$(MAKE) greenplum_path INSTLOC=$(INSTLOC)
	#@$(MAKE) groupSession INSTLOC=$(INSTLOC)
	@$(MAKE) mkgpfdist INSTCFLAGS=$(INSTCFLAGS)
	@$(MAKE) mkdbgen INSTLOC=$(INSTLOC)
	#@$(MAKE) mgmtcopy INSTLOC=$(INSTLOC)
	@$(MAKE) mkpgbouncer INSTLOC=$(INSTLOC) BUILDDIR=$(BUILDDIR)
	@$(MAKE) mkpgbench INSTLOC=$(INSTLOC) BUILDDIR=$(BUILDDIR)
	@$(MAKE) mkgpperfmon INSTLOC=$(INSTLOC) BUILDDIR=$(BUILDDIR)
	@$(MAKE) mkplatform INSTLOC=$(INSTLOC)
	if [ "$(findstring $(BLD_ARCH),$(SERVER_PLATFORMS))" != "" ]; then \
	  $(MAKE) mkgphdfs INSTLOC=$(INSTLOC) && \
	  $(MAKE) mkorafce INSTLOC=$(INSTLOC) && \
	  $(MAKE) mkpljava INSTLOC=$(INSTLOC); \
	fi
	cd extensions/gpmapreduce/ && $(MAKE) install
	cd extensions/gp_filedump/ && $(MAKE) install
	@$(MAKE) copydocs INSTLOC=$(INSTLOC)
	@$(MAKE) copylibs INSTLOC=$(INSTLOC)
	@$(MAKE) clients INSTLOC=$(INSTLOC) CLIENTINSTLOC=$(CLIENTINSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(CLIENTINSTLOC)
	@$(MAKE) connectivity INSTLOC=$(INSTLOC) CONNECTIVITYINSTLOC=$(CONNECTIVITYINSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(CONNECTIVITYINSTLOC)
	@$(MAKE) loaders INSTLOC=$(INSTLOC) LOADERSINSTLOC=$(LOADERSINSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(LOADERSINSTLOC)
	## NOTE: The copyoptimizerlibs taret must be called after the
	## clients and connectivity targets (above) so as to keep the
	## optimizer libs out of their respective distributions.
	@$(MAKE) copyoptimizerlibs INSTLOC=$(INSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(CLIENTINSTLOC)
	@$(MAKE) copylicense INSTLOC=$(INSTLOC) \
                         CLIENTINSTLOC=$(CLIENTINSTLOC) \
                         CONNECTIVITYINSTLOC=$(CONNECTIVITYINSTLOC) \
                         LOADERSINSTLOC=$(LOADERSINSTLOC)
	@$(MAKE) gppkg INSTLOC=$(INSTLOC) GPPKGINSTLOC=$(GPPKGINSTLOC)
endef

ifeq "$(BLD_GPDB_BUILDSET)" "aix_subset"
define BUILD_STEPS
	rm -rf $(INSTLOC)
	cd $(BUILDDIR)/src/include/ && $(MAKE) install
	@$(MAKE) mkgpfdist INSTLOC=$(INSTLOC)
	@$(MAKE) mkdbgen INSTLOC=$(INSTLOC)
	@$(MAKE) mgmtcopy INSTLOC=$(INSTLOC)
	@$(MAKE) copylibs INSTLOC=$(INSTLOC)
	@$(MAKE) loaders INSTLOC=$(INSTLOC) LOADERSINSTLOC=$(LOADERSINSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(LOADERSINSTLOC)
	@$(MAKE) connectivity INSTLOC=$(INSTLOC) CONNECTIVITYINSTLOC=$(CONNECTIVITYINSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(CONNECTIVITYINSTLOC)
endef
endif

ifeq "$(BLD_GPDB_BUILDSET)" "partial"
define BUILD_STEPS
	rm -f installcheck-good_SKIP
	rm -f installcheck-bugbuster_SKIP
	rm -rf $(INSTLOC)
	@if [ "$(findstring win,$(BLD_ARCH))" = "win" ]; then \
	    echo "Modifying timezone Makefile for crosscompile."; \
	    perl -pi -e 's,./zic,zic,' ../src/timezone/Makefile; \
	fi
	cd $(BUILDDIR)/gpMgmt/ && $(MAKE) generate_greenplum_path_file
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/parser/gram.h
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/utils/fmgroids.h
	cd $(BUILDDIR)/src/port/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/scripts/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/psql/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/pg_config/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_dump
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_dump$(EXE_EXT) $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_dumpall
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_dumpall$(EXE_EXT) $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_restore
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_restore$(EXE_EXT) $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/interfaces/ecpg/ && $(MAKE) install
	cd $(BUILDDIR)/src/include/ && $(MAKE) install
	cd $(BUILDDIR)/src/interfaces/libpq/ && $(MAKE) install
	if [ "$(findstring win,$(BLD_ARCH))" != "win" ]; then \
	  cd extensions/gpmapreduce/ && $(MAKE) install ; \
	fi
	cd extensions && $(MAKE) mkgpfdist
	cp -p extensions/gpfdist/gpfdist$(EXE_EXT) $(INSTLOC)/bin/
	cp -p $(GPMGMT)/bin/gpload $(INSTLOC)/bin/gpload
	cp -p $(GPMGMT)/bin/gpload.py $(INSTLOC)/bin/gpload.py
	$(MAKE) copylibs INSTLOC=$(INSTLOC)
	$(MAKE) clients INSTLOC=$(INSTLOC) CLIENTINSTLOC=$(CLIENTINSTLOC)
	$(MAKE) connectivity INSTLOC=$(INSTLOC) CONNECTIVITYINSTLOC=$(CONNECTIVITYINSTLOC)
	if [ "$(findstring hpux_ia64,$(BLD_ARCH))" = "hpux_ia64" ]; then \
	  cd $(GPMGMT)/bin && $(MAKE) pygresql LDFLAGS="" INSTLOC=$(INSTLOC) ; \
	elif [ "$(findstring win,$(BLD_ARCH))" != "win" ]; then \
	  cd $(GPMGMT)/bin && $(MAKE) pygresql INSTLOC=$(INSTLOC) ; \
	fi
	$(MAKE) loaders INSTLOC=$(INSTLOC) LOADERSINSTLOC=$(LOADERSINSTLOC)
	touch installcheck-good_SKIP
	touch installcheck-bugbuster_SKIP
endef
endif

#---------------------------------------------------------------------
# environment for later builds inside the source tree
#
# run this to create an environment-loading script, source it,
# and be able to build inside the source tree with the same
# results as building from here at the top
#---------------------------------------------------------------------

env.sh:
	echo "export BLD_ARCH=$(BLD_ARCH)" > env.sh
	echo "export NO_M64=$(NO_M64)" >> env.sh
	echo "export CC=\"$(strip $(BLD_CC) $(BLD_CFLAGS))\"" >> env.sh
	echo "export PATH=$(PATH)" >> env.sh
	echo "export $(BLD_WHERE_THE_LIBRARY_THINGS_ARE)=$($(BLD_WHERE_THE_LIBRARY_THINGS_ARE))" >> env.sh
ifneq "$(PERL_DIR)" ""
	echo "export PERL5LIB=$(PERL_DIR)/lib/site_perl/5.12.4/x86_64-linux:$(PERL_DIR)/lib/site_perl/5.12.4:$(PERL_DIR)/lib/5.12.4/x86_64-linux:$(PERL_DIR)/lib/5.12.4" >> env.sh
endif

#---------------------------------------------------------------------
# rsync + build
#---------------------------------------------------------------------

both : dbg opt ;

dbg:
	rsync -rlvt --exclude=devel*/ --exclude=dist*/ --exclude=LOG.* ./ devel$(BLD_BITS)/
	cd devel$(BLD_BITS) && $(MAKE) BLD_HOME=`pwd` PG_LANG=true devel

opt:
	rsync -rlvt --exclude=devel*/ --exclude=dist*/ --exclude=LOG.* ./ dist$(BLD_BITS)/
	cd dist$(BLD_BITS) && $(MAKE) GPROOT=`pwd` PG_LANG=true dist

#---------------------------------------------------------------------
# Development builds
#---------------------------------------------------------------------

devel : INSTCFLAGS=$(DEBUGFLAGS)
devel : INSTLOC=$(DEVPATH)
devel : CLIENTINSTLOC=$(CLIENTDEVPATH)
devel : CONNECTIVITYINSTLOC=$(CONNECTIVITYDEVPATH)
devel : LOADERSINSTLOC=$(LOADERSDEVPATH)
devel : GPPERFMONINSTLOC=$(GPPERFMONDEVPATH)
devel : GPPKGINSTLOC=$(GPPKGDEVPATH)
devel : CONFIGFLAGS+= --enable-cassert --enable-debug --enable-testutils --enable-debugbreak --enable-depend
ifdef ENABLE_VPATH_BUILD
devel : BUILDDIR=Debug
devel : ISCONFIG=$(BUILDDIR)/GNUmakefile
devel : gccVersionCheck HOMEDEP Debug/GNUmakefile
else
devel : ISCONFIG=../GNUmakefile
devel : gccVersionCheck HOMEDEP $(GPPGDIR)/GNUmakefile
endif
	$(BUILD_STEPS)

#---------------------------------------------------------------------
# Release builds
#---------------------------------------------------------------------

dist : INSTCFLAGS=$(OPTFLAGS)
dist : INSTLOC=$(DISTPATH)
dist : CLIENTINSTLOC=$(CLIENTDISTPATH)
dist : CONNECTIVITYINSTLOC=$(CONNECTIVITYDISTPATH)
dist : LOADERSINSTLOC=$(LOADERSDISTPATH)
dist : GPPERFMONINSTLOC=$(GPPERFMONDISTPATH)
dist : GPPKGINSTLOC=$(GPPKGDISTPATH)
ifdef ENABLE_VPATH_BUILD
dist : BUILDDIR=Release
dist : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist : gccVersionCheck GPROOTDEP RECONFIG Release/GNUmakefile
else
dist : ISCONFIG=../GNUmakefile
dist : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)
ifneq "$(BLD_GPDB_BUILDSET)" "partial"
	@$(MAKE) qautils-tarball INSTLOC=$(INSTLOC)
endif

dist_prof : INSTCFLAGS=$(PROFFLAGS)
dist_prof : INSTLOC=$(DISTPATH)
dist_prof : CLIENTINSTLOC=$(CLIENTDISTPATH)
dist_prof : CONNECTIVITYINSTLOC=$(CONNECTIVITYDISTPATH)
dist_prof : LOADERSINSTLOC=$(LOADERSDISTPATH)
dist_prof : GPPERFMONINSTLOC=$(GPPERFMONDISTPATH)
dist_prof : CONFIGFLAGS+= --enable-testutils 
ifdef ENABLE_VPATH_BUILD
dist_prof : BUILDDIR=Release
dist_prof : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist_prof : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
else
dist_prof : ISCONFIG=../GNUmakefile
dist_prof : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

dist_faultinj : CLIENTINSTLOC=$(CLIENTDISTPATH)
dist_faultinj : CONNECTIVITYINSTLOC=$(CONNECTIVITYDISTPATH)
dist_faultinj : LOADERSINSTLOC=$(LOADERSDISTPATH)
dist_faultinj : CONFIGFLAGS+= --enable-testutils --enable-debugbreak
ifdef ENABLE_VPATH_BUILD
dist_faultinj : BUILDDIR=Release
dist_faultinj : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist_faultinj : gccVersionCheck GPROOTDEP RECONFIG Release/GNUmakefile
else
dist_faultinj : ISCONFIG=../GNUmakefile
dist_faultinj : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

#---------------------------------------------------------------------
# clientTools
#---------------------------------------------------------------------
#
# The clients target builds GPDB [f]or a subset of files needed for client
# platforms.  It also builds clientTools-specific items like the ODBC drivers.
#

ifneq "$(GPROOT)" ""
CLIENT_HOME_DIR=$(GPROOT)
CONNECTIVITY_HOME_DIR=$(GPROOT)
LOADERS_HOME_DIR=$(GPROOT)
else
CLIENT_HOME_DIR=$(BLD_HOME)
CONNECTIVITY_HOME_DIR=$(BLD_HOME)
dist_faultinj : CLIENTINSTLOC=$(CLIENTDISTPATH)
LOADERS_HOME_DIR=$(BLD_HOME)
endif

CLIENTINSTLOC=$(CLIENT_HOME_DIR)/greenplum-clients
CLIENTINSTLOC_BIN=$(CLIENTINSTLOC)/bin
CLIENTINSTLOC_LIB=$(CLIENTINSTLOC)/lib
CLIENTINSTLOC_INCLUDE=$(CLIENTINSTLOC)/include
CLIENTINSTLOC_DOCS=$(CLIENTINSTLOC)/docs
CLIENTINSTLOC_OPENSOURCE=$(CLIENTINSTLOC)/thirdparty

LOADERSINSTLOC=$(LOADERS_HOME_DIR)/greenplum-loaders
LOADERSINSTLOC_BIN=$(LOADERSINSTLOC)/bin
LOADERSINSTLOC_BINEXT=$(LOADERSINSTLOC)/bin/ext/
LOADERSINSTLOC_EXT=$(LOADERSINSTLOC)/ext
LOADERSINSTLOC_LIB=$(LOADERSINSTLOC)/lib
LOADERSINSTLOC_LIB_PWARE=$(LOADERSINSTLOC)/lib/pware
LOADERSINSTLOC_DOCS=$(LOADERSINSTLOC)/docs
LOADERSINSTLOC_OPENSOURCE=$(LOADERSINSTLOC)/thirdparty

CONNECTIVITYINSTLOC=$(CONNECTIVITY_HOME_DIR)/greenplum-connectivity
CONNECTIVITYINSTLOC_JDBC=$(CONNECTIVITYINSTLOC)/drivers/jdbc
CONNECTIVITYINSTLOC_ODBC=$(CONNECTIVITYINSTLOC)/drivers/odbc
CONNECTIVITYINSTLOC_LIB=$(CONNECTIVITYINSTLOC)/lib
CONNECTIVITYINSTLOC_INCLUDE=$(CONNECTIVITYINSTLOC)/include
CONNECTIVITYINSTLOC_DOCS=$(CONNECTIVITYINSTLOC)/docs
CONNECTIVITYINSTLOC_OPENSOURCE=$(CONNECTIVITYINSTLOC)/thirdparty

ifneq "$(findstring win,$(BLD_ARCH))" "win"
DOC_TYPE=Unix
SCRIPT=.sh
else
DOC_TYPE=Win
SCRIPT=.bat
endif

define tmpCLIENT_SCRIPTS
endef
CLIENT_SCRIPTS = $(strip $(tmpCLIENT_SCRIPTS))

define tmpCLIENT_SQLDOCS
	html
endef
CLIENT_SQLDOCS = $(strip $(tmpCLIENT_SQLDOCS))

define tmpCLIENT_FILESET
	pg_dump$(EXE_EXT)
	pg_dumpall$(EXE_EXT)
	psql$(EXE_EXT)
endef
define Unix_CLIENT_FILESET
	gpmapreduce$(EXE_EXT)
endef
CLIENT_FILESET = $(strip $(tmpCLIENT_FILESET) $($(DOC_TYPE)_CLIENT_FILESET))

# yes, the doubled gpload lines are correct
# this gives win32, win64 just gpload.py, as they want, and wrappers on the unix platforms
define tmpLOADERS_FILESET_BIN
	gpfdist$(EXE_EXT)
	gpload$(PY_EXT)
	gpload.py
endef
LOADERS_FILESET_BIN = $(strip $(tmpLOADERS_FILESET_BIN))

define tmpLOADERS_FILESET_BINEXT
	pygresql
	yaml
endef
LOADERS_FILESET_BINEXT = $(strip $(tmpLOADERS_FILESET_BINEXT))

# moved BLD_PYTHON definition and inclusion in path and library path to Makefile.global

# define python file subset to ship by platform, as desired
BLD_PYTHON_FILESET=.
aix5_ppc_32_PYTHON_FILESET=bin/python* lib/python* include/python*
aix5_ppc_64_PYTHON_FILESET=bin/python* lib/python* include/python*
ifneq "$($(BLD_ARCH)_PYTHON_FILESET)" ""
BLD_PYTHON_FILESET=$($(BLD_ARCH)_PYTHON_FILESET)
endif

ifneq "$(findstring $(BLD_ARCH),win32 win64)" ""
BLD_OS=Windows
else
BLD_OS:=$(shell uname -s)
endif
AIX_LOADERS_LIBS=libbz2.a libz.a libpq.a liblber*.a libldap*.a libyaml*.a
Darwin_LOADERS_LIBS=libcrypto.*.dylib libssl.*.dylib libpq.*.dylib* libkrb5.*.dylib libcom_err.*.dylib libldap_r-*.dylib libk5crypto.*.dylib libkrb5support.*.dylib liblber-*.dylib
HP-UX_LOADERS_LIBS=libcrypto.so* libssl.so.* libz.so* libpq.so* libapr*
Linux_LOADERS_LIBS=libcrypto.so* libssl.so.* libz.so* libpq.so* libkrb5.so* libcom_err.so* libk5crypto.so* libkrb5support.so* liblber*.so* libldap_r-*so* libgssapi_krb5.so*
SunOS_LOADERS_LIBS=libcrypto.so* libssl.so.* libgcc_s.so.1 libz.so* libpq.so* libk5crypto.so* libkrb5support.so* liblber*.so* libldap_r-*so* libcom_err.so* libkrb5.so*
Windows_LOADERS_LIBS=comerr32.dll gssapi32.dll k5sprt32.dll kclnt32.dll kfwlogon.dll krb4cred.dll krb4cred_en_us.dll krb524.dll krb5_32.dll krb5cred.dll krb5cred_en_us.dll krbcc32.dll krbv4w32.dll leashw32.dll nidmgr32.dll wshelp32.dll xpprof32.dll libpq.dll
define tmpLOADERS_FILESET_LIB
	$($(BLD_OS)_LOADERS_LIBS)
endef
LOADERS_FILESET_LIB = $(strip $(tmpLOADERS_FILESET_LIB))

hpux_ia64_LOADERS_LIBS_GCC=libgcc_s.so libgcc_s.so.0
hpux_ia64_LOADERS_LIBS_GCC_LOC=/opt/hp-gcc/lib/hpux64

FILESET_LIB_GCC=$($(BLD_ARCH)_LOADERS_LIBS_GCC)
FILESET_LIB_GCC_LOC=$($(BLD_ARCH)_LOADERS_LIBS_GCC_LOC)

aix5_ppc_32_LOADERS_LIBS_PERZL=libz.a
aix5_ppc_64_LOADERS_LIBS_PERZL=libz.so libz.so.1 libz.so.1.2.4 libcrypto.so libcrypto.so.0.9.8 libssl.so libssl.so.0.9.8

aix5_ppc_32_LOADERS_LIBS_PERZL_LOC=/opt/freeware/lib
aix5_ppc_64_LOADERS_LIBS_PERZL_LOC=/opt/pware64/lib

LOADERS_FILESET_LIB_PERZL=$($(BLD_ARCH)_LOADERS_LIBS_PERZL)
LOADERS_FILESET_LIB_PERZL_LOC=$($(BLD_ARCH)_LOADERS_LIBS_PERZL_LOC)

AIX_LOADERS_LIBS_PWARE=libbz2.a libiconv.a libintl.a libncurses.a libpanel.a libsqlite3.a
LOADERS_FILESET_LIB_PWARE=$($(BLD_OS)_LOADERS_LIBS_PWARE)

ifeq "$(MPP_ARCH)" ""
sol10_x86_64_MPP_ARCH=SOL-x86_64
sol10_x86_32_MPP_ARCH=SOL-i386
sol10_sparc_MPP_ARCH=SOL10-sparc
sol10_sparc_64_MPP_ARCH=SOL10-sparc_64
sol10_sparc_32_MPP_ARCH=SOL10-sparc_32
rhel5_x86_64_MPP_ARCH=RHEL5-x86_64
rhel6_x86_64_MPP_ARCH=RHEL6-x86_64
osx106_x86_MPP_ARCH=OSX-i386
ifneq "$($(BLD_ARCH)_MPP_ARCH)" ""
export MPP_ARCH=$($(BLD_ARCH)_MPP_ARCH)
else
export MPP_ARCH=$(BLD_ARCH)
endif
endif

ifeq "$(INSTLOC)" ""
INSTLOC=$(GPDIR)
endif

# define build flags for GP Optimizer UDFs library
ifeq (Darwin, $(BLD_OS))
	GPOPT_UDF_FLAGS := -m32 -dynamiclib -flat_namespace -undefined dynamic_lookup
else
	GPOPT_UDF_FLAGS := -m64 -shared
endif

VERSION:=$(shell [ -f ../VERSION ] && perl -pe 's, ,-,g' ../VERSION)

ifeq "$(findstring win,$(BLD_ARCH))" "win"
BLD_PACKAGING_PID:=$(shell ssh build@harpy 'echo $$$$')
endif

connectivity :
	@if [ ! -z "$(CONNECTIVITYINSTLOC)" ] && [ "$(CONNECTIVITYINSTLOC)" != "/" ]; then \
	  echo "rm -rf $(CONNECTIVITYINSTLOC)"; \
	  rm -rf $(CONNECTIVITYINSTLOC); \
	fi
ifeq "$(findstring connectivity,$(BLD_TARGETS))" ""
	# ---- build and packaging of the Connectivity package is currently disabled;
	# ---- set BLD_TARGETS="connectivity" to enable them.
else
	# ---- load LD_LIBRARY_PATH with the GPDB library set and build the ODBC drivers ----
	cd $(INSTLOC)/ && \
	  . ./greenplum_path.sh; \
	  cd $(BLD_TOP)/client/odbc/src && \
	    $(MAKE) all
	# ---- copy GPDB fileset ----
	mkdir -p $(CONNECTIVITYINSTLOC_LIB)
	(cd $(INSTLOC)/lib && $(TAR) cf - *) | (cd $(CONNECTIVITYINSTLOC_LIB)/ && $(TAR) xvpf -)$(check_pipe_for_errors)
	mkdir -p $(CONNECTIVITYINSTLOC_INCLUDE)
	(cd $(INSTLOC)/include && $(TAR) cf - *) | (cd $(CONNECTIVITYINSTLOC_INCLUDE)/ && $(TAR) xvpf -)$(check_pipe_for_errors)
	# ---- copy JDBC fileset ----
	mkdir -p $(CONNECTIVITYINSTLOC_JDBC)
	cp -fp client/jdbc/*.jar $(CONNECTIVITYINSTLOC_JDBC)
	# ---- copy ODBC fileset ----
	mkdir -p $(CONNECTIVITYINSTLOC_ODBC)
	(cd client/odbc/src/greenplum-connectivity/odbc/ && $(TAR) cf - --exclude=*.la *) | (cd $(CONNECTIVITYINSTLOC_ODBC)/ && $(TAR) xvf -)$(check_pipe_for_errors)
	# ---- copy scripts fileset ----
	mkdir -p $(CONNECTIVITYINSTLOC)
	cp -f client/scripts/greenplum_$@_path$(SCRIPT) $(CONNECTIVITYINSTLOC)/

	# ---- copy license files ----
	cp ${BLD_TOP}/../LICENSE $(CONNECTIVITYINSTLOC)
	cp ${BLD_TOP}/../NOTICE $(CONNECTIVITYINSTLOC)

	# ---- create the installer ----
	$(warn MPP_ARCH=$(MPP_ARCH))

	# ---- updating the version in required packages ----
	@$(MAKE) set_scripts_version INSTLOC=$(CONNECTIVITYINSTLOC)

ifneq "$(findstring win,$(BLD_ARCH))" "win"
	cd client/install/src/unix/ && \
	  $(MAKE) TAR=$(TAR) PRODUCT=Connectivity PACKAGE=greenplum-connectivity README=GPConnectUnix VERSION=$(VERSION) INSTALL_SRC=$(CONNECTIVITYINSTLOC) INSTALL_DIR=../../../..
else
	if [ "$(BLD_PACKAGING_PID)" = "" ]; then \
	    echo "ERROR: BLD_PACKAGING_PID must have a value for Windows packaging operations; check your Makefile"; \
	    exit 1; \
	fi
	ssh build@harpy 'if [ -d $(BLD_PACKAGING_PID) ]; then rm -rf $(BLD_PACKAGING_PID); fi'
	ssh build@harpy 'mkdir -p $(BLD_PACKAGING_PID)/packaging/src/windows'
	scp -r client/install/src/windows/* build@harpy:$(BLD_PACKAGING_PID)/packaging/src/windows/
	scp -r $(CONNECTIVITYINSTLOC) build@harpy:$(BLD_PACKAGING_PID)/packaging/
	ssh build@harpy 'cd $(BLD_PACKAGING_PID)/packaging/src/windows/ && make PACKAGE=greenplum-$@ VERSION=$(VERSION) SRCDIR=../../$(notdir $(CONNECTIVITYINSTLOC))'
	scp build@harpy:$(BLD_PACKAGING_PID)/packaging/greenplum-$@-$(VERSION)-WinXP-x86_32.msi .
	chmod 755 greenplum-$@-$(VERSION)-WinXP-x86_32.msi
	ssh build@harpy 'rm -rf $(BLD_PACKAGING_PID)'
endif
endif

clients :
	if [ ! -z "$(CLIENTINSTLOC)" ] && [ "$(CLIENTINSTLOC)" != "/" ]; then rm -rf $(CLIENTINSTLOC); fi
ifeq "$(findstring clients,$(BLD_TARGETS))" ""
	# ---- build and packaging of the Clients package is currently disabled;
	# ---- set BLD_TARGETS="clients" to enable them.
else
	# ---- copy GPDB fileset ----
	mkdir -p $(CLIENTINSTLOC_BIN)
	(cd $(INSTLOC)/bin && $(TAR) cf - $(CLIENT_FILESET)) | (cd $(CLIENTINSTLOC_BIN)/ && $(TAR) xpf -)$(check_pipe_for_errors)
	mkdir -p $(CLIENTINSTLOC_INCLUDE)
	(cd $(INSTLOC)/include && $(TAR) cf - *) | (cd $(CLIENTINSTLOC_INCLUDE)/ && $(TAR) xpf -)$(check_pipe_for_errors)
	mkdir -p $(CLIENTINSTLOC_LIB)
	(cd $(INSTLOC)/lib && $(TAR) cf - *) | (cd $(CLIENTINSTLOC_LIB)/ && $(TAR) xpf -)$(check_pipe_for_errors)
ifneq "$(FILESET_LIB_GCC)" ""
	(cd $(CLIENTINSTLOC_LIB)/ && $(TAR) cf - -C $(FILESET_LIB_GCC_LOC) $(FILESET_LIB_GCC) | $(TAR) xpf -)$(check_pipe_for_errors)
endif
	# ---- copy docs fileset ----
	mkdir -p $(CLIENTINSTLOC_DOCS)
	cp -rpf client/docs/release/$(CLIENT_SQLDOCS) $(CLIENTINSTLOC_DOCS)/
	# ---- copy scripts fileset ----
	mkdir -p $(CLIENTINSTLOC)
	cp -f client/scripts/greenplum_$@_path$(SCRIPT) $(CLIENTINSTLOC)/

	# ---- copy license files ----
	cp ${BLD_TOP}/../LICENSE $(CLIENTINSTLOC)
	cp ${BLD_TOP}/../NOTICE $(CLIENTINSTLOC)

	# ---- create the installer ----
	$(warn MPP_ARCH=$(MPP_ARCH))

	# ---- Updating the version in required packages ----
	@$(MAKE) set_scripts_version INSTLOC=$(CLIENTINSTLOC)

ifneq "$(findstring win,$(BLD_ARCH))" "win"
	cd client/install/src/unix/ && \
	  $(MAKE) TAR=$(TAR) PRODUCT=Clients PACKAGE=greenplum-clients README=GPClientToolsUnix VERSION=`perl -pe 's, ,-,g' ../../../../../VERSION` INSTALL_SRC=$(CLIENTINSTLOC) INSTALL_DIR=../../../..
else
	if [ "$(BLD_PACKAGING_PID)" = "" ]; then \
	    echo "ERROR: BLD_PACKAGING_PID must have a value for Windows packaging operations; check your Makefile"; \
	    exit 1; \
	fi
	ssh build@harpy 'if [ -d $(BLD_PACKAGING_PID) ]; then rm -rf $(BLD_PACKAGING_PID); fi'
	ssh build@harpy 'mkdir -p $(BLD_PACKAGING_PID)/packaging/src/windows'
	scp -r client/install/src/windows/* build@harpy:$(BLD_PACKAGING_PID)/packaging/src/windows/
	scp -r $(CLIENTINSTLOC) build@harpy:$(BLD_PACKAGING_PID)/packaging/
	ssh build@harpy 'cd $(BLD_PACKAGING_PID)/packaging/src/windows/ && make PACKAGE=greenplum-$@ VERSION=$(VERSION) SRCDIR=../../$(notdir $(CLIENTINSTLOC))'
	scp build@harpy:$(BLD_PACKAGING_PID)/packaging/greenplum-$@-$(VERSION)-WinXP-x86_32.msi .
	chmod 755 greenplum-$@-$(VERSION)-WinXP-x86_32.msi
	ssh build@harpy 'rm -rf $(BLD_PACKAGING_PID)'
endif
endif

loaders :
	if [ ! -z "$(LOADERSINSTLOC)" ] && [ "$(LOADERSINSTLOC)" != "/" ]; then rm -rf $(LOADERSINSTLOC); fi
ifeq "$(findstring loaders,$(BLD_TARGETS))" ""
	# ---- build and packaging of the Loaders package is currently disabled;
	# ---- set BLD_TARGETS="loaders" to enable them.
else
	# ---- copy GPDB fileset ----
	mkdir -p $(LOADERSINSTLOC_BIN)
	(cd $(INSTLOC)/bin/ && $(TAR) cf - $(LOADERS_FILESET_BIN)) | (cd $(LOADERSINSTLOC_BIN)/ && $(TAR) xpf -)$(check_pipe_for_errors)
	mkdir -p $(LOADERSINSTLOC_BINEXT)
	(cd $(GPMGMT)/bin/ext/ && $(TAR) cf - $(LOADERS_FILESET_BINEXT)) | (cd $(LOADERSINSTLOC_BINEXT)/ && $(TAR) xpf -)$(check_pipe_for_errors)
ifeq "$(findstring win,$(BLD_ARCH))" "win"
	# ---- copy pygresql from ext/win32 ----
	(cd $(BLD_THIRDPARTY)/win32 && $(TAR) cf - pygresql) | (cd $(LOADERSINSTLOC_BINEXT)/ && $(TAR) xpf -)$(check_pipe_for_errors)
endif
ifneq "$($(BLD_ARCH)_PYTHON)" ""
	mkdir -p $(LOADERSINSTLOC_EXT)/python
	(cd $($(BLD_ARCH)_PYTHON) && $(TAR) cf - $(BLD_PYTHON_FILESET)) | (cd $(LOADERSINSTLOC_EXT)/python/ && $(TAR) xpf -)$(check_pipe_for_errors)
endif
	mkdir -p $(LOADERSINSTLOC_LIB)
ifneq "$(LOADERS_FILESET_LIB)" ""
	(cd $(INSTLOC)/lib/ && $(TAR) cf - $(LOADERS_FILESET_LIB)) | (cd $(LOADERSINSTLOC_LIB)/ && $(TAR) xpf -)$(check_pipe_for_errors)
endif
ifneq "$(FILESET_LIB_GCC)" ""
	(cd $(LOADERSINSTLOC_LIB)/ && $(TAR) cf - -C $(FILESET_LIB_GCC_LOC) $(FILESET_LIB_GCC) | $(TAR) xpf -)$(check_pipe_for_errors)
endif
ifneq "$(LOADERS_FILESET_LIB_PERZL)" ""
	# include libraries from www.perzl.org needed by the pware Python-2.6.1 build
	mkdir -p $(LOADERSINSTLOC_LIB_PWARE)
	($(TAR) cf - -C $(LOADERS_FILESET_LIB_PERZL_LOC) $(LOADERS_FILESET_LIB_PERZL)) | ($(TAR) xpf - -C $(LOADERSINSTLOC_LIB_PWARE))$(check_pipe_for_errors)
endif
ifneq "$(LOADERS_FILESET_LIB_PWARE)" ""
	# include libraries from pware needed by the pware Python-2.6.1 build
	mkdir -p $(LOADERSINSTLOC_LIB_PWARE)
	($(TAR) cf - -C $($(BLD_ARCH)_PYTHON)/lib $(LOADERS_FILESET_LIB_PWARE)) | ($(TAR) xpf - -C $(LOADERSINSTLOC_LIB_PWARE))$(check_pipe_for_errors)
endif
ifneq "$(findstring $(BLD_ARCH),sol10_sparc_32)" ""
	(cd /usr/local/lib/ && $(TAR) cf - libiconv.so* libexpat.so*) | (cd $(LOADERSINSTLOC_LIB)/ && $(TAR) xpf -)$(check_pipe_for_errors)
endif
	# ---- copy scripts fileset ----
	mkdir -p $(LOADERSINSTLOC)
	cp -f client/scripts/greenplum_$@_path$(SCRIPT) $(LOADERSINSTLOC)/

	# ---- copy license files ----
	cp ${BLD_TOP}/../LICENSE $(LOADERSINSTLOC)
	cp ${BLD_TOP}/../NOTICE $(LOADERSINSTLOC)

	# ---- create the installer ----

	# ---- updating the version in required packages ----
	@$(MAKE) set_scripts_version INSTLOC=$(LOADERSINSTLOC)

ifneq "$(findstring win,$(BLD_ARCH))" "win"
	cd client/install/src/unix/ && \
	  $(MAKE) TAR=$(TAR) PRODUCT=Loaders PACKAGE=greenplum-loaders README=GPLoadToolsUnix VERSION=`perl -pe 's, ,-,g' ../../../../../VERSION` INSTALL_SRC=$(LOADERSINSTLOC) INSTALL_DIR=../../../..
else
	if [ "$(BLD_PACKAGING_PID)" = "" ]; then \
	    echo "ERROR: BLD_PACKAGING_PID must have a value for Windows packaging operations; check your Makefile"; \
	    exit 1; \
	fi
	ssh build@harpy 'if [ -d $(BLD_PACKAGING_PID) ]; then rm -rf $(BLD_PACKAGING_PID); fi'
	ssh build@harpy 'mkdir -p $(BLD_PACKAGING_PID)/packaging/src/windows'
	scp -r client/install/src/windows/* build@harpy:$(BLD_PACKAGING_PID)/packaging/src/windows/
	scp -r $(LOADERSINSTLOC) build@harpy:$(BLD_PACKAGING_PID)/packaging/
	ssh build@harpy 'cd $(BLD_PACKAGING_PID)/packaging/src/windows/ && make PACKAGE=greenplum-$@ VERSION=$(VERSION) SRCDIR=../../$(notdir $(LOADERSINSTLOC))'
	scp build@harpy:$(BLD_PACKAGING_PID)/packaging/greenplum-$@-$(VERSION)-WinXP-x86_32.msi .
	chmod 755 greenplum-$@-$(VERSION)-WinXP-x86_32.msi
	ssh build@harpy 'rm -rf $(BLD_PACKAGING_PID)'
endif
endif

#---------------------------------------------------------------------
# Platform Verification Kit (PVK)
#   formerly checkTools
#
# The pvk target builds a subset of the GPDB package that includes
# just the tools used to verify the configuration of a set of GPDB servers.
#---------------------------------------------------------------------

PVK_SCRIPTS = \
	bin/diskqual.ksh	\
	bin/generate_load_tpch.pl	\
	bin/gpscp			\
	bin/gpssh			\
	bin/gpcheckos			\
	bin/gpcheckperf			\
	bin/run_operator_tests.pl	\
	$(NULL)

PVK_FILESET = \
	$(CHECKTOOLS_SCRIPTS)	\
	bin/lib/gplib.py	\
	bin/lib/pxssh.py	\
	bin/lib/pexpect.py	\
	bin/lib/gpsys.py	\
	bin/lib/multidd		\
	bin/lib/netserver	\
	bin/lib/stream		\
	$(NULL)

pvk : INSTCFLAGS=$(OPTFLAGS)
pvk : INSTLOC=$(DISTPATH)
pvk : gccVersionCheck version GPROOTDEP
	cd $(GPMGMT)/bin && $(MAKE) stream
	mkdir -p $(INSTLOC)/bin/lib
	@for file in $(PVK_FILESET); do \
	    echo "cp -p $(GPMGMT)/$${file} $(INSTLOC)/$${file}"; \
	    cp -p $(GPMGMT)/$${file} $(INSTLOC)/$${file}; \
	done
	perl putversion $(foreach script,$(PVK_SCRIPTS),$(INSTLOC)/$(script))

#---------------------------------------------------------------------
# Set Version
#---------------------------------------------------------------------

SET_VERSION_SCRIPTS = \
	bin/gpactivatestandby \
	bin/gpaddmirrors \
	bin/gpbitmapreindex \
	bin/gpcheckos \
	bin/gpcheckperf \
	bin/gpcrondump.py \
	bin/gpdbrestore \
	bin/gpdeletesystem \
	bin/gpexpand \
	bin/gpfilespace \
	bin/gpinitstandby \
	bin/gpinitsystem \
	bin/gpload.py \
	bin/gplogfilter \
	bin/gpmigrator \
	bin/gpmigrator_mirror \
	bin/gpmovemirrors \
	bin/gprebuildsystem \
	bin/gprecoverseg \
	bin/gpreload \
	bin/gpscp \
	bin/gpsizecalc \
	bin/gpskew \
	bin/gpssh \
	bin/gpssh-exkeys \
	bin/gpstart \
	bin/gpstate \
	bin/gpstop \
	bin/gpsys1 \
	bin/lib/gpcheckcat \
	sbin/gpaddconfig.py \
	sbin/gpchangeuserpassword \
	sbin/gpcheck_hostdump \
	sbin/gpcleansegmentdir.py \
	sbin/gpfixuserlimts \
	sbin/gpgetstatususingtransition.py \
	sbin/gprepairmirrorseg.py \
	sbin/gpsegstart.py \
	sbin/gpsegstop.py \
	sbin/gpsegtoprimaryormirror.py \
	sbin/gpsetdbid.py \
	sbin/gpsuspend.py \
	sbin/gpupgrademirror.py \
	lib/python/gppylib/programs/clsAddMirrors.py \
	lib/python/gppylib/programs/clsHostCacheLookup.py \
	lib/python/gppylib/programs/clsInjectFault.py \
	lib/python/gppylib/programs/clsRecoverSegment.py \
	lib/python/gppylib/programs/clsSystemState.py \
	lib/python/gppylib/programs/gppkg.py \
	lib/python/gppylib/programs/kill.py \
	lib/python/gppylib/programs/verify.py \
	lib/python/gppylib/mainUtils.py \
	$(NULL)

set_scripts_version : 
	@for file in $(SET_VERSION_SCRIPTS); do \
	    if [ -f $(INSTLOC)/$${file} ]; then \
	        perl $(BLD_TOP)/../putversion $(INSTLOC)/$${file} ; \
	    fi ; \
	done

#---------------------------------------------------------------------
# Internal functions
#---------------------------------------------------------------------

groupSession:
	@cd $(GPMGMT)/bin && $(MAKE)

mkgpfdist:
	@cd extensions && $(MAKE) mkgpfdist OPT="$(INSTCFLAGS)" INSTLOC=$(INSTLOC)
	cp -p extensions/gpfdist/gpfdist$(EXE_EXT) $(INSTLOC)/bin/

mkgphdfs:
	@cd extensions && $(MAKE) mkgphdfs OPT="$(INSTCFLAGS)" INSTLOC=$(INSTLOC)

mkpljava:
	@cd extensions && $(MAKE) mkpljava OPT="$(INSTCFLAGS)" INSTLOC=$(INSTLOC)

mkorafce:
	@cd extensions && $(MAKE) mkorafce OPT="$(INSTCFLAGS)" INSTLOC=$(INSTLOC)

mkdbgen:
	@cd extensions && $(MAKE) mkdbgen OPT="$(INSTCFLAGS)" INSTLOC=$(INSTLOC)
	cp extensions/tpch/source/dbgen extensions/tpch/source/dists.dss $(INSTLOC)/bin

mkpgbench:
	@cd $(BUILDDIR)/contrib/pgbench && $(MAKE) install

mkpgbouncer: 
ifeq "$(shell uname -s)" "Darwin"
	echo "pgbouncer can't build on Mac"
else
	@if [ ! -f extensions/pgbouncer/source/configure ]; then cd extensions/pgbouncer/source && ./autogen.sh ;fi
	@cd extensions/pgbouncer/source && ./configure --with-libevent=$(BLD_TOP)/ext/$(BLD_ARCH) --prefix=$(INSTLOC) --enable-evdns
	$(MAKE) -C extensions/pgbouncer/source install
endif

mkgpperfmon:
	@cd $(GPPERFMON) && $(MAKE) BUILDDIR=$(BUILDDIR)
	cp -p gpperfmon/src/gpmon/gpmmon gpperfmon/src/gpmon/gpsmon $(INSTLOC)/bin
	cp -p gpperfmon/src/gpmon/gpperfmoncat.sh $(INSTLOC)/bin
	cp -p gpperfmon/src/gpmon/gpmon_catqrynow.py $(INSTLOC)/sbin
	cp -p gpperfmon/src/gpmon/gpperfmon_install $(INSTLOC)/bin
	mkdir -p $(INSTLOC)/lib/gpperfmon
	cp -p gpperfmon/src/gpmon/gpperfmon3.sql $(INSTLOC)/lib/gpperfmon
	cp -p gpperfmon/src/gpmon/gpperfmon4.sql $(INSTLOC)/lib/gpperfmon
	cp -p gpperfmon/src/gpmon/gpperfmon41.sql $(INSTLOC)/lib/gpperfmon
	cp -p gpperfmon/src/gpmon/gpperfmon42.sql $(INSTLOC)/lib/gpperfmon
	cp -p gpperfmon/src/gpmon/gpperfmonC.sql $(INSTLOC)/lib/gpperfmon

mkplatform:
	@cd $(PLATFORM) && $(MAKE)
	cp -p platform/gpnetbench/gpnetbenchServer $(INSTLOC)/bin/lib
	cp -p platform/gpnetbench/gpnetbenchClient $(INSTLOC)/bin/lib
	mkdir -p $(INSTLOC)/etc
	cp -p platform/gpcheck/gpcheck.cnf $(INSTLOC)/etc/

copydocs :
	@if [ ! -d $($INSTLOC)/docs ] ; then mkdir -p $(INSTLOC)/docs; fi
	@if [ ! -d $(GPDOCDIR) ] ; then \
	    echo "Error Copying Documentation: $(GPDOCDIR) is not present"; \
	else  echo "Copying Documentation" ;  \
	    if [ -d $(INSTLOC)/doc/postgresql/contrib ] ; then \
	        mkdir -p $(INSTLOC)/docs/contrib ; \
	        echo "Copying postgresql contrib documentation" ; \
		cp -r $(INSTLOC)/doc/postgresql/contrib/* $(INSTLOC)/docs/contrib ; \
	    fi ; \
	    echo "Copying $(BLD_TOP)/extensions/pljava/PLJAVA_README" ; \
	    mkdir -p $(INSTLOC)/share/postgresql/pljava ; \
	    cp -p $(BLD_TOP)/extensions/pljava/PLJAVA_README $(INSTLOC)/share/postgresql/pljava ; \
	fi
	@if [ -d $(INSTLOC)/doc ] ; then \
	    echo "Removing $(INSTLOC)/doc" ; \
	    rm -rf $(INSTLOC)/doc ; \
	fi

mgmtcopy :
	#Copy the management utilities
	mkdir -p $(INSTLOC)/bin
	mkdir -p $(INSTLOC)/lib
	mkdir -p $(INSTLOC)/lib/python	
	mkdir -p $(INSTLOC)/sbin
	#Setup /lib/python contents
	cp -rp $(GPMGMT)/bin/gppylib $(INSTLOC)/lib/python
	cp -rp $(GPMGMT)/bin/ext/* $(INSTLOC)/lib/python
	cp -rp $(GPMGMT)/bin $(INSTLOC)
ifeq "$(findstring $(BLD_ARCH),$(GPPKG_PLATFORMS))" ""
	@echo "Removing gppkg from distribution"
	rm -f $(INSTLOC)/bin/gppkg
endif
	cp -rp $(GPMGMT)/sbin/* $(INSTLOC)/sbin/.
	cp -p extensions/gpfdist/gpfdist$(EXE_EXT) $(INSTLOC)/bin/
	cp $(GPPGDIR)/src/test/regress/*.pl $(INSTLOC)/bin
	if [ ! -d ${INSTLOC}/docs ] ; then mkdir ${INSTLOC}/docs ; fi
	if [ -d $(GPMGMT)/doc ]; then cp -rp $(GPMGMT)/doc $(INSTLOC)/docs/cli_help; fi
	if [ -d $(GPMGMT)/demo/gpmapreduce ]; then \
	  mkdir -p $(INSTLOC)/demo; \
	  $(TAR) -C $(GPMGMT)/demo -czf $(INSTLOC)/demo/gpmapreduce.tar.gz gpmapreduce; \
	fi
	if [ -d $(GPMGMT)/demo/gpfdist_transform ]; then \
	  mkdir -p $(INSTLOC)/demo; \
	  $(TAR) -C $(GPMGMT)/demo -czf $(INSTLOC)/demo/gpfdist_transform.tar.gz gpfdist_transform; \
	fi
	$(MAKE) set_scripts_version INSTLOC=$(INSTLOC)
	# Remove unwanted files.
	rm -rf $(INSTLOC)/bin/CVS
	rm -rf $(INSTLOC)/doc/CVS
	rm -rf $(INSTLOC)/bin/ext
	rm -rf $(INSTLOC)/bin/pythonSrc
	rm -rf $(INSTLOC)/bin/Makefile
	rm -rf $(INSTLOC)/bin/lib/CVS
	rm -rf $(INSTLOC)/bin/lib/.p4ignore
	rm -rf $(INSTLOC)/bin/src
	rm -f $(INSTLOC)/bin/gpchecksubnetcfg
	echo "`date` -- INFO: Removing $(INSTLOC)/bin/gpexpandsystem"
	rm -f $(INSTLOC)/bin/gpexpandsystem
	rm -rf $(INSTLOC)/bin/gppylib
	find $(INSTLOC)/lib/python/gppylib -name test -type d | xargs rm -rf

BLD_LDD=ldd
ifneq "$($(BLD_ARCH)_LDD)" ""
BLD_LDD=$($(BLD_ARCH)_LDD)
endif

BLD_LDD_FILTER=|awk '{print $$3}' | sort -u | perl -p -i -e 's,^(.*\.so.*),\1\*,'
ifneq "$($(BLD_ARCH)_LDD_FILTER)" ""
BLD_LDD_FILTER=$($(BLD_ARCH)_LDD_FILTER)
endif

libcopy=(cd $(1) && $(TAR) cf - $(2)) | (cd $(3) && $(TAR) xvf -)$(check_pipe_for_errors)

.PHONY: copy-rsa-libs copy-nbu-libs

copy-rsa-libs:
	# Copy rsa lockbox libs - supported only on rhel (required for Data
ifeq "$(BLD_ARCH)" "rhel5_x86_64"
	cp -fpr $(BLD_THIRDPARTY_LIB_DIR)/rsa_csp $(INSTLOC)/lib/;
endif

copy-nbu-libs:
	#Copy NetBackup libs
ifeq "$(BLD_ARCH)" "rhel5_x86_64"
	rm -f $(INSTLOC)/lib/libxbsa64.so;
	rm -f $(INSTLOC)/lib/libnbclientcST.so;
	rm -f $(INSTLOC)/lib/libnbbasecST.so;
	rm -f $(INSTLOC)/lib/libvxcPBXST.so;
	cp -fpr $(BLD_THIRDPARTY_LIB_DIR)/../Netbackup/nbu71 $(INSTLOC)/lib/;
	cp -fpr $(BLD_THIRDPARTY_LIB_DIR)/../Netbackup/nbu75 $(INSTLOC)/lib/;
endif

copylibs : thirdparty-dist copy-rsa-libs copy-nbu-libs
	if [ `uname -s` = 'SunOS' ]; then cp `ldd $(INSTLOC)/bin/psql | grep gcc | head -1 | awk '{print $$3}'` $(INSTLOC)/lib; fi
	if [ `uname -s` = 'Darwin' ] ; then \
	    for lib in `otool -L $(INSTLOC)/bin/psql | egrep 'libssl|libcrypto|libcom_err|libkrb5|curl|readline|numa' | awk '{print $$1}'`; do \
	        if [ x"`dirname $$lib`" = x"$(INSTLOC)/lib" ]; then continue; fi; \
	        if [ -f $(INSTLOC)/lib/`basename $$lib` -a ! -w $(INSTLOC)/lib/`basename $$lib` ]; then chmod u+w $(INSTLOC)/lib/`basename $$lib`; fi; \
	        if [ -f $(BLD_THIRDPARTY_LIB_DIR)/`basename $$lib` ]; then \
	            echo "cp -p $(BLD_THIRDPARTY_LIB_DIR)/`basename $$lib` $(INSTLOC)/lib/" ; \
	            cp -p $(BLD_THIRDPARTY_LIB_DIR)/`basename $$lib` $(INSTLOC)/lib/ ; \
	        else \
	            echo "cp -p $$lib $(INSTLOC)/lib/" ; \
	            cp -p $$lib $(INSTLOC)/lib/ ; \
	        fi ; \
	    done ; \
	fi
	if [ `uname -s` != 'Darwin' -a `uname -s` != 'AIX' ] ; then \
	    for lib in `ldd $(INSTLOC)/bin/psql | egrep 'libssl|libcrypto|libcom_err|curl|readline|numa' | awk '{print $$3}' | sort -u`; do \
	        if [ x"`dirname $$lib`" = x"$(INSTLOC)/lib" ]; then continue; fi; \
	        if [ -f $(INSTLOC)/lib/`basename $$lib` -a ! -w $(INSTLOC)/lib/`basename $$lib` ]; then chmod u+w $(INSTLOC)/lib/`basename $$lib`; fi; \
	        echo "cp -p $$lib $(INSTLOC)/lib" ; \
	        cp -p $$lib $(INSTLOC)/lib ; \
	    done ; \
	fi
	if [ `uname -s` = 'AIX' ] ; then \
	    for lib in `ldd $(INSTLOC)/bin/psql | egrep 'libssl|libcrypto|libcom_err|curl|readline|numa' | awk -F\( '{print $$1}' | sort -u`; do \
	        if [ x"`dirname $$lib`" = x"$(INSTLOC)/lib" ]; then continue; fi; \
	        if [ -f $(INSTLOC)/lib/`basename $$lib` -a ! -w $(INSTLOC)/lib/`basename $$lib` ]; then chmod u+w $(INSTLOC)/lib/`basename $$lib`; fi; \
	        echo "cp -p $$lib $(INSTLOC)/lib" ; \
	        cp -p $$lib $(INSTLOC)/lib ; \
	    done ; \
	    for lib in `ldd $(INSTLOC)/bin/gpfdist | egrep 'libbz2|libz' | awk -F\( '{print $$1}' | sort -u`; do \
	        if [ x"`dirname $$lib`" = x"$(INSTLOC)/lib" ]; then continue; fi; \
	        if [ -f $(INSTLOC)/lib/`basename $$lib` -a ! -w $(INSTLOC)/lib/`basename $$lib` ]; then chmod u+w $(INSTLOC)/lib/`basename $$lib`; fi; \
	        echo "cp -p $$lib $(INSTLOC)/lib" ; \
	        cp -p $$lib $(INSTLOC)/lib ; \
	    done ; \
	fi
	if [ `uname -s` != 'AIX' -a `uname -s` != 'Darwin' ] ; then \
	    lib_wildcards=`$(BLD_LDD) $(INSTLOC)/bin/lib/gpcheckdb | egrep 'libstdc\+\+' $(BLD_LDD_FILTER)` export lib_wildcards; \
	    if [ -z "$${lib_wildcards}" ]; then exit 0; fi; \
	    lib_list=`ls $${lib_wildcards}` export lib_list; \
	    for lib in $${lib_list}; do \
	        echo -n "Copying $$lib to $(subst $(CURDIR)/,,$(INSTLOC)/lib)..."; \
	        if [ x"`dirname $$lib`" = x"$(INSTLOC)/lib" ]; then echo "already there."; continue; fi; \
	        if [ -f $(INSTLOC)/lib/`basename $$lib` -a ! -w $(INSTLOC)/lib/`basename $$lib` ]; then chmod u+w $(INSTLOC)/lib/`basename $$lib`; fi; \
	        (cd `dirname $$lib` && $(TAR) cf - `basename $$lib`) | (cd $(INSTLOC)/lib/ && $(TAR) xpf -)$(check_pipe_for_errors); \
	        echo "done."; \
	    done; \
	fi
	# Create the python directory to flag to build scripts that python has been handled
	mkdir -p $(INSTLOC)/ext/python
	@if [ ! -z "$(BLD_PYTHON)" ]; then \
	    echo "Copying python, $(BLD_PYTHON_FILESET), from $(BLD_PYTHON) into $(INSTLOC)/ext/python..."; \
	    (cd $(BLD_PYTHON) && $(TAR) cf - $(BLD_PYTHON_FILESET)) | (cd $(INSTLOC)/ext/python/ && $(TAR) xpf -); \
		echo "...DONE"; \
	else \
	    echo "INFO: Python not found on this platform, $(BLD_ARCH), not copying it into the GPDB package."; \
	fi
	mkdir -p $(INSTLOC)/etc
	mkdir -p $(INSTLOC)/include
ifeq "$(findstring $(BLD_ARCH),aix5_ppc_32 aix5_ppc_64 win32 win64)" ""
	# Copy curl header file
	cp -rp $(BLD_THIRDPARTY_INCLUDE_DIR)/curl $(INSTLOC)/include/
	# Copy in openssl files needed by gpperfmon
endif
ifeq "$(findstring $(BLD_ARCH),aix5_ppc_32 win32 win64)" ""
ifneq "$(BLD_ARCH)" "osx104_x86"
	cp -rp $(BLD_THIRDPARTY_BIN_DIR)/openssl $(INSTLOC)/bin/
	cp -rp $(BLD_THIRDPARTY_INCLUDE_DIR)/openssl $(INSTLOC)/include/
	cp -rp $(BLD_THIRDPARTY_DIR)/ssl/openssl.cnf $(INSTLOC)/etc/
else
	cp -rp /usr/bin/openssl $(INSTLOC)/bin/
	cp -rp /usr/include/openssl $(INSTLOC)/include/
	cp -rp /System/Library/OpenSSL/openssl.cnf $(INSTLOC)/etc/
endif
endif

	# Copy glider stack trace capture tool
	-cp -rp $(BLD_THIRDPARTY_BIN_DIR)/glider $(INSTLOC)/sbin/

ifeq "$(BLD_ARCH)" "win32"
	# Copy kfw dlls
	cp -rp $(BLD_TOP)/ext/win32/kfw-3-2-2/lib/*dll $(INSTLOC)/lib/
endif

## ----------------------------------------------------------------------
## Copy Optimizer (orca) specific libraries to INSTLOC.  It must be
## called after the clients and connectivity targets have been
## executed to populate their respective installation directories.
## The clients and connectivity targets copy the entire contents of
## the db libs directory.  There is no need to copy the optimizer
## files and thus it has been broken out into it's own target.
## ----------------------------------------------------------------------

copyoptimizerlibs :
ifneq "$(BLD_GPDB_BUILDSET)" "partial"
ifeq "$(ORCA_CONFIG)" "--enable-orca"
	# Copy GP Optimizer libraries 
	echo "Copying Orca libraries";
	for b in libnaucrates libgpdbcost libgpopt; do \
		rm -f $(INSTLOC)/lib/$$b.$(LDSFX); \
		echo "Copying $$b"; \
		cp  $(OPTIMIZER)/$$b/$(OBJDIR_DEFAULT)/$$b.$(LDSFX) $(INSTLOC)/lib ; \
	done
	rm -f $(INSTLOC)/lib/libgpos.$(LDSFX);
	cp $(LIBGPOS_LIBDIR)/libgpos.$(LDSFX) $(INSTLOC)/lib;
	rm -f $(INSTLOC)/lib/libxerces-c-3.1.$(LDSFX);
	cp $(XERCES_LIBDIR)/libxerces-c-3.1.$(LDSFX) $(INSTLOC)/lib;

	# Copy DXL translators library
	echo "Copy DXL translators library";	
	cp $(BLD_TOP)/../src/backend/gpopt/libdxltranslators.$(LDSFX) $(INSTLOC)/lib;

	cd $(LIBSTDC++_LIBDIR); tar cvf - * | (cd $(INSTLOC)/lib; tar xfp -)

	# Create GP Optimizer UDFs library  
	echo "Create GP Optimizer UDFs library";
	rm -f $(INSTLOC)/lib/libgpoptudf.$(LDSFX);
	g++ $(GPOPT_UDF_FLAGS) -L$(INSTLOC)/lib -lgpos -lnaucrates -lgpdbcost -lgpopt -o $(INSTLOC)/lib/libgpoptudf.$(LDSFX)  ./../src/backend/gpopt/utils/funcs.o ;
endif
endif

greenplum_path:
	mkdir -p $(INSTLOC)
	unset LIBPATH; \
	  releng/generate-greenplum-path.sh $(INSTLOC) > $(INSTLOC)/greenplum_path.sh

copylicense:
	for proddir in $(INSTLOC) $(CLIENTINSTLOC) $(CONNECTIVITYINSTLOC) $(LOADERSINSTLOC); do \
	   if [ -d $${proddir} ]; then \
	      echo cp -v ${BLD_TOP}/LICENSE $${proddir}; \
	      echo cp -v ${BLD_TOP}/NOTICE  $${proddir}; \
	   fi; \
	done

HOMEDEP :
	@ #If the HOME variable isn't set, then execute the usage() path
	@if [ x"$(HOME)" = x ]; then $(MAKE) HOMEFAIL; fi

GPROOTDEP :
	@ #If the GPROOT variable isn't set, then execute the usage() path
	@if [ x"$(GPROOT)" = x ]; then $(MAKE) GPROOTFAIL; fi

GPROOTFAIL :
	@echo
	@echo "	The GPROOT path variable is not set."
	@echo
	@echo "	You must decide where you will install to, and set GPROOT to that location, prior to"
	@echo "	installing the GP module with this Makefile."
	@echo
	@exit 1

HOMEFAIL :
	@echo
	@echo "	The HOME environment variable is not set. Please set it to continue."
	@echo
	@exit 1

##
## Create qautils tarball.  This file contains the utils required to
## support cdbfast runs.
##

qautils-tarball:
	@$(TAR) -zcf releng/qautils-$(MPP_ARCH).tar.gz -C $(INSTLOC) $(shell cat releng/QAUTILS_FILES.txt)

#---------------------------------------------------------------------
# Cleanup
#---------------------------------------------------------------------

clean :
#	@if [ ! -f $(ISCONFIG) ]; then echo "GPDB root not configured, no need to clean it"; fi
	@if [ -d Debug -a -f Debug/GNUmakefile ]; then cd Debug; $(MAKE) clean; fi
	@if [ -d Release -a -f Release/GNUmakefile ]; then cd Release; $(MAKE) clean; fi
	@if [ -d $(GPPGDIR) -a -f $(GPPGDIR)/GNUmakefile ]; then cd $(GPPGDIR); $(MAKE) clean; fi
	@if [ -d $(GPMGMT)/bin ]; then cd $(GPMGMT)/bin; $(MAKE) clean; fi
	@if [ -d extensions/gpfdist ]; then cd extensions/gpfdist; $(MAKE) clean; fi
	@if [ -d $(GPPERFMON) ]; then cd $(GPPERFMON); $(MAKE) clean; fi
	@if [ -d client/odbc/src/Makefile ]; then cd client/odbc/src; $(MAKE) clean; fi

distclean :
#	@if [ ! -f $(ISCONFIG) ]; then echo "GPDB root not configured, no need to clean it"; fi
	@if [ -d Debug -a -f Debug/GNUmakefile ]; then cd Debug; $(MAKE) distclean; fi
	@if [ -d Release -a -f Release/GNUmakefile ]; then cd Release; $(MAKE) distclean; fi
	@if [ -d $(GPPGDIR) -a -f $(GPPGDIR)/GNUmakefile ]; then cd $(GPPGDIR); $(MAKE) distclean; fi
	@if [ -d $(GPMGMT)/bin ]; then cd $(GPMGMT)/bin; $(MAKE) distclean; fi
	@if [ -d extensions/gpfdist ]; then cd extensions/gpfdist; $(MAKE) clean; fi
	@if [ -d $(GPPERFMON) ]; then cd $(GPPERFMON); $(MAKE) clean; fi
	@if [ -d client/odbc/src/Makefile ]; then cd client/odbc/src; $(MAKE) clean; fi
	@rm -f VERSION
	@rm -f env.sh

distprep : $(ISCONFIG)
	@cd $(BUILDDIR) && $(MAKE) distprep

#---------------------------------------------------------------------
# Regression tests
#---------------------------------------------------------------------

bouncer:
	@cd $(BUILDDIR)/src/test/regress && $(MAKE) testbouncer

installcheck-good :
	@if [ ! -f $(ISCONFIG) ]; then echo "not configured, cannot check"; exit 1; fi
	@cd $(BUILDDIR)/src/test/regress && $(MAKE) installcheck-good
	@#cd $(BUILDDIR)/src/test/regress && $(MAKE) testbouncer
	@cd extensions && $(MAKE) installcheck

installcheck-parallel :
	@if [ ! -f $(ISCONFIG) ]; then echo "not configured, cannot check"; exit 1; fi
	@cd $(BUILDDIR)/src/test/regress && $(MAKE) installcheck-parallel
	@cd extensions && $(MAKE) installcheck

installcheck-olaptest :
	@if [ ! -f $(ISCONFIG) ]; then echo "not configured, cannot check"; exit 1; fi
	@cd $(BUILDDIR)/src/test/regress && $(MAKE) installcheck-olaptest

installcheck-bugbuster :
	@if [ ! -f $(ISCONFIG) ]; then echo "not configured, cannot check"; exit 1; fi
	@cd $(BUILDDIR)/src/test/regress && $(MAKE) installcheck-bugbuster

#---------------------------------------------------------------------
# More internal functions
#---------------------------------------------------------------------

gccVersionCheck :
	@echo "Checking GCC version" ; \
	$(BLD_CC) --version > /dev/null 2>&1 ; \
	if [ $$? -ne 0 ] ; then \
	    echo "Error running $(BLD_CC) --version" ; \
	    $(BLD_CC) --version ; \
	    exit 1 ; \
	fi
	@$(BLD_CC) --version | head -n 1 | cut -d' ' -f3 | egrep '[0-3]\.[0-4]\.[0-1]' > /dev/null 2>&1 ; \
	if [ $$? -eq 0 ] ; then \
	    echo "Error: GPDB must be compiled with GCC 3.4.2 or later." ; \
	    echo "Current GCC version:" ; \
	    $(BLD_CC) --version ; \
	    exit 1 ; \
	fi

## ----------------------------------------------------------------------

ifeq ($(BLD_ARCH),$(filter $(BLD_ARCH),rhel6_x86_64 suse11_x86_64))
GPPKG_SOURCES = \
	../src/pl/plperl \
	$(NULL)
else
GPPKG_SOURCES = \
	../contrib/pgcrypto \
	../src/pl/plperl \
	../src/bin/pg_dump/cdb/ddboost \
	extensions/postgis-2.0.3 \
	extensions/pljava \
	extensions/plr \
	$(NULL)
	ifneq ($(wildcard ../contrib/pgpool),)
		GPPKG_SOURCES := ../contrib/pgpool $(GPPKG_SOURCES)
	endif
	ifneq ($(wildcard ../contrib/dtm),)
		GPPKG_SOURCES := ../contrib/dtm $(GPPKG_SOURCES)
	endif
	ifneq ($(wildcard ../contrib/pg_arcgis),)
		GPPKG_SOURCES := ../contrib/pg_arcgis $(GPPKG_SOURCES)
	endif
	ifneq ($(wildcard ../contrib/pg_osm),)
		GPPKG_SOURCES := ../contrib/pg_osm $(GPPKG_SOURCES)
	endif
endif

## ----------------------------------------------------------------------

gppkg:
ifeq "$(findstring gppkg,$(BLD_TARGETS))" ""
	# ---- build and packaging of gppkgs are currently disabled
	# ---- set BLD_TARGETS="gppkg" to enable them.
else
ifeq "$(findstring $(BLD_ARCH),$(GPPKG_PLATFORMS))" ""
	@echo ""
	@echo "The gppkg build and packaging processes are not supported"
	@echo "on this platform: $(BLD_ARCH)"
	@echo ""
else
	for source in $(GPPKG_SOURCES); do \
		echo "";\
		echo "====================================================================================================";\
		echo "Starting build of gppkg: $${source}" ; \
		echo "----------------------------------------------------------------------------------------------------";\
		$(MAKE) -C $${source}/package INSTLOC=$(INSTLOC) BLD_ARCH=`$(BLD_TOP)/releng/set_bld_arch.sh`; \
		RET_VAL=$$? ; \
		if [ $$RET_VAL -ne 0 ] ; then \
			echo "Error building gppkg: $${source}" ; \
			exit $$RET_VAL; \
		fi; \
		echo "";\
		echo "====================================================================================================";\
		echo "Completed build of gppkg: $${source}" ;\
		echo "____________________________________________________________________________________________________";\
		cp $${source}/package/*.gppkg $(GPPKGINSTLOC); \
	done
endif
endif

ifeq ($(BLD_ARCH),$(filter $(BLD_ARCH),rhel6_x86_64 suse11_x86_64))
GPPKG_ROOT_DIR = \
	../src/pl/plperl \
	$(NULL)
else
GPPKG_ROOT_DIR = \
	../contrib/pgcrypto \
	../src/pl/plperl \
	$(NULL)
endif

installcheck-gppkg:
	touch installcheck-gppkg.log
ifeq "$(findstring gppkg,$(BLD_TARGETS))" ""
    # ---- build and packaging of gppkgs are currently disabled
    # ---- set BLD_TARGETS="gppkg" to enable them.
else
ifeq "$(findstring $(BLD_ARCH),$(GPPKG_PLATFORMS))" ""
	@echo ""
	@echo "The gppkg build and packaging processes are not supported"
	@echo "on this platform: $(BLD_ARCH)"
	@echo ""
else
	@for root_gppkg in $(GPPKG_ROOT_DIR); do \
		echo "----------------------------------------------------------------------"; \
		echo "installing gppkg for $${root_gppkg}"; \
		echo "----------------------------------------------------------------------"; \
		source $$GPHOME/greenplum_path.sh && $(MAKE) -C $${root_gppkg}/package install BLD_ARCH=`$(BLD_TOP)/releng/set_bld_arch.sh` INSTLOC=$$GPHOME; \
		RET_VAL=$$? ; \
		if [ $$RET_VAL -ne 0 ] ; then \
			echo "Error running install: $${root_gppkg}" ; \
			exit $$RET_VAL; \
		fi; \
	done

	source $$GPHOME/greenplum_path.sh && gpstop -ar

	@for root_gppkg in $(GPPKG_ROOT_DIR); do \
		echo "----------------------------------------------------------------------"; \
		echo "running installcheck for $${root_gppkg}"; \
		echo "----------------------------------------------------------------------"; \
		installcheck_dir=""; \
		if [ -d "$${root_gppkg}/source" ]; then \
			installcheck_dir="source"; \
		fi; \
		source $$GPHOME/greenplum_path.sh && $(MAKE) -C $${root_gppkg}/$${installcheck_dir} installcheck >> installcheck-gppkg.log; \
		RET_VAL=$$? ; \
		if [ $$RET_VAL -ne 0 ] ; then \
			echo "Error running installcheck-gppkg : $${root_gppkg}/$${installcheck_dir}" ; \
			exit $$RET_VAL; \
		fi; \
	done
endif
endif

## ----------------------------------------------------------------------

.PHONY : unittest-check
unittest-check:
	$(MAKE) -C ../src/backend unittest-check
	$(MAKE) -C ../src/bin unittest-check
